Imports System
Imports System.Configuration
Imports System.Data
Imports DotNetNuke.Services.Search
Imports DotNetNuke
Imports DotNetNuke.Common.Utilities
Imports DotNetNuke.Common.Globals
Imports System.XML
Imports System.Web

Namespace effority.WF_HTML

    Public Class HtmlTextController

        Implements Entities.Modules.ISearchable
        Implements Entities.Modules.IPortable
        'Implements Entities.Modules.IUpgradeable

        Private Const MAX_DESCRIPTION_LENGTH As Integer = 100

#Region "Public Methods"

        Public Function GetHtmlTextLastVersion(ByVal moduleId As Integer) As HtmlTextInfo

            Return CBO.FillObject(DataProvider.Instance().ExecuteReader("effority_WF_HtmlText_GetLastVersion", moduleId), GetType(HtmlTextInfo))

        End Function
        Public Function GetHtmlTextActiveVersion(ByVal moduleId As Integer) As HtmlTextInfo

            Return CBO.FillObject(DataProvider.Instance().ExecuteReader("effority_WF_HtmlText_GetActiveVersion", moduleId), GetType(HtmlTextInfo))

        End Function
        Public Function GetHtmlTextByVersion(ByVal moduleId As Integer, ByVal Version As Integer) As HtmlTextInfo
           
            Return CBO.FillObject(DataProvider.Instance().ExecuteReader("effority_WF_HtmlText_GetByVersion", moduleId, Version), GetType(HtmlTextInfo))

        End Function
        Public Function GetHtmlTextById(ByVal Id As Integer) As HtmlTextInfo

            Return CBO.FillObject(DataProvider.Instance().ExecuteReader("effority_WF_HtmlText_GetById", Id), GetType(HtmlTextInfo))

        End Function
        Public Function GetHtmlTextAllVersions(ByVal moduleId As Integer) As ArrayList

            Return CBO.FillCollection(DataProvider.Instance().ExecuteReader("effority_WF_HtmlText_GetAllVersions", moduleId), GetType(HtmlTextInfo))

        End Function

        Public Sub SetHtmlTextActive(ByVal Id As Integer)

            DataProvider.Instance().ExecuteNonQuery("effority_WF_HtmlText_SetActive", Id)

        End Sub

        Public Sub DeleteHtmlText(ByVal Id As Integer)

            DataProvider.Instance().ExecuteNonQuery("effority_WF_HtmlText_DeleteById", Id)

        End Sub

        Public Sub AddHtmlText(ByVal objText As HtmlTextInfo)

            DataProvider.Instance.ExecuteNonQuery("effority_WF_HtmlText_Add", objText.ModuleId, objText.DeskTopHTML, objText.DesktopSummary, objText.CreatedByUser)

        End Sub

        Public Sub UpdateHtmlText(ByVal objText As HtmlTextInfo)

            DataProvider.Instance().ExecuteNonQuery("effority_WF_HtmlText_Update", objText.ModuleId, objText.DeskTopHTML, objText.DesktopSummary, objText.CreatedByUser)

        End Sub

        Public Function NewVersionAvaiable(ByVal ModuleId As Integer) As Boolean

            Return Not CBool(DataProvider.Instance().ExecuteScalar("effority_WF_SearchForNewVersion", ModuleId))

        End Function

#End Region

#Region "Optional Interfaces"

        Public Function GetSearchItems(ByVal ModInfo As Entities.Modules.ModuleInfo) As DotNetNuke.Services.Search.SearchItemInfoCollection Implements Entities.Modules.ISearchable.GetSearchItems

            Dim SearchItemCollection As New SearchItemInfoCollection

            Dim HtmlText As HtmlTextInfo = Me.GetHtmlTextActiveVersion(ModInfo.ModuleID)

            If Not HtmlText Is Nothing Then
                'DesktopHTML is encoded in the Database so Decode before Indexing
                Dim strDesktopHtml As String = HttpUtility.HtmlDecode(HtmlText.DeskTopHTML)

                'Get the description string
                Dim strDescription As String = HtmlUtils.Shorten(HtmlUtils.Clean(strDesktopHtml, False), MAX_DESCRIPTION_LENGTH, "...")

                Dim SearchItem As SearchItemInfo = New SearchItemInfo(ModInfo.ModuleTitle, strDescription, HtmlText.CreatedByUser, HtmlText.CreatedDate, ModInfo.ModuleID, "", HtmlText.DesktopSummary & " " & strDesktopHtml, "", Null.NullInteger)
                SearchItemCollection.Add(SearchItem)
            End If
            Return SearchItemCollection

        End Function

        Public Function ExportModule(ByVal ModuleID As Integer) As String Implements Entities.Modules.IPortable.ExportModule

            Dim strXML As String = ""

            Dim objHtmlText As HtmlTextInfo = GetHtmlTextActiveVersion(ModuleID)
            If Not objHtmlText Is Nothing Then
                strXML += "<htmltext>"
                strXML += "<desktophtml>" & DotNetNuke.Common.Utilities.XmlUtils.XMLEncode(objHtmlText.DeskTopHTML) & "</desktophtml>"
                strXML += "<desktopsummary>" & DotNetNuke.Common.Utilities.XmlUtils.XMLEncode(objHtmlText.DesktopSummary) & "</desktopsummary>"
                strXML += "</htmltext>"
            End If

            Return strXML

        End Function

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' ImportModule implements the IPortable ImportModule Interface
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <param name="ModuleID">The Id of the module to be imported</param>
        ''' <history>
        '''		[cnurse]	11/15/2004	documented
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Public Sub ImportModule(ByVal ModuleID As Integer, ByVal Content As String, ByVal Version As String, ByVal UserId As Integer) Implements Entities.Modules.IPortable.ImportModule

            Dim xmlHtmlText As XmlNode = GetContent(Content, "htmltext")

            Dim objText As HtmlTextInfo = New HtmlTextInfo

            objText.ModuleId = ModuleID
            objText.CreatedByUser = UserId

            'Get the original item
            Dim objTextOld As HtmlTextInfo = Me.GetHtmlTextActiveVersion(ModuleID)

            'See if there was an item already
            If objTextOld Is Nothing Then
                'Need to insert the imported item
                objText.DeskTopHTML = xmlHtmlText.SelectSingleNode("desktophtml").InnerText
                objText.DesktopSummary = xmlHtmlText.SelectSingleNode("desktopsummary").InnerText
                AddHtmlText(objText)
            Else
                'Need to appende the imported item to the existing item
                objText.DeskTopHTML = objTextOld.DeskTopHTML & xmlHtmlText.SelectSingleNode("desktophtml").InnerText
                objText.DesktopSummary = objTextOld.DesktopSummary & xmlHtmlText.SelectSingleNode("desktopsummary").InnerText
                UpdateHtmlText(objText)
            End If

        End Sub

        'Public Function UpgradeModule(ByVal Version As String) As String Implements Entities.Modules.IUpgradeable.UpgradeModule

        '    InitPermissions()

        '    Return Version

        'End Function

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        '''     adds the module specific permissions
        ''' </summary>
        ''' -----------------------------------------------------------------------------
        Public Shared Sub InitPermissions()

            Dim moduleDefId As Integer

            Dim pc As New DotNetNuke.Security.Permissions.PermissionController

            Dim permissionList As ArrayList = pc.GetPermissionByCodeAndKey(Permission.PermissionCode, Permission.PermissionKey)

            If permissionList Is Nothing Or permissionList.Count = 0 Then

                Dim dc As New DotNetNuke.Entities.Modules.DesktopModuleController

                Dim desktopInfo As DotNetNuke.Entities.Modules.DesktopModuleInfo

                desktopInfo = dc.GetDesktopModuleByModuleName(Definition.ModuleName)

                Dim mc As New DotNetNuke.Entities.Modules.Definitions.ModuleDefinitionController
                Dim mInfo As DotNetNuke.Entities.Modules.Definitions.ModuleDefinitionInfo

                mInfo = mc.GetModuleDefinitionByName(desktopInfo.DesktopModuleID, Definition.ModuleName)
                moduleDefId = mInfo.ModuleDefID

                Dim p As New DotNetNuke.Security.Permissions.PermissionInfo

                p.ModuleDefID = moduleDefId
                p.PermissionCode = Permission.PermissionCode
                p.PermissionKey = Permission.PermissionKey
                p.PermissionName = Permission.PermissionName

                pc.AddPermission(p)


            End If



        End Sub


#End Region

    End Class

End Namespace
